package com.lagou.client;

import com.lagou.Exception.AgeException;
import com.lagou.Exception.IdException;
import com.lagou.model.Exam;
import com.lagou.model.ExamMessage;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 编程实现客户端的主界面绘制和相应功能的实现
 */
public class ClientView {
    /**
     * 为了可以使用输入输出流，采用合成复用原则实现
     */
    private ClientInitClose cic;

    /**
     * 学员信息列表
     */
    private List<User> list = new ArrayList<>();

    /**
     * 通过构造方法实现成员变量的初始化
     *
     * @param cic
     */
    public ClientView(ClientInitClose cic) {
        this.cic = cic;
    }

    /**
     * 自定义成员方法实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException, AgeException, IdException {
        while (true) {
            System.out.println("--------学员信息管理系统----------");
            System.out.println("         1 ：学员系统            ");
            System.out.println("         2 ：管理员系统          ");
            System.out.println("         3 ：退出系统            ");
            System.out.println("-------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    while (true) {
                        System.out.println("正在进入学员系统..");
                        System.out.println("请输入您要选择的模块:");
                        System.out.println("     1.用户模块    ");
                        System.out.println("     2.考试模块    ");
                        System.out.println("     3.返回       ");
                        int choose1 = ClientScanner.getScanner().nextInt();
                        switch (choose1) {
                            case 1:
                                System.out.println("正在进入学员系统————用户模块..");
                                while (true) {
                                    System.out.println("请输入您要选择的功能模块：");
                                    System.out.println("       1.用户登录     ");
                                    System.out.println("       2.修改密码      ");
                                    System.out.println("       3.退出         ");
                                    int choose2 = ClientScanner.getScanner().nextInt();
                                    switch (choose2) {
                                        case 1:
                                            ClientUserLogin();
                                            break;
                                        case 2:
                                            ClientUpdatePassword();
                                            break;
                                        case 3:
                                            break;
                                        default:
                                            break;
                                    }
                                    //跳出功能模块选择的循环
                                    if (choose2 == 3) {
                                        break;
                                    }
                                }
                                break;
                            case 2:
                                System.out.println("正在进入学员系统————考试模块..");
                                while (true) {
                                    System.out.println("请输入您要选择的功能模块");
                                    System.out.println("      1.开始考试     ");
                                    System.out.println("      2.查询成绩     ");
                                    System.out.println("      3.退出        ");
                                    int choose3 = ClientScanner.getScanner().nextInt();
                                    switch (choose3) {
                                        case 1:
                                            startExam();
                                            break;
                                        case 2:
                                            selectGrade();
                                            break;
                                        case 3:
                                            break;
                                        default:
                                            break;
                                    }
                                    if (choose3 == 3) {
                                        break;
                                    }
                                }
                                break;
                            case 3:
                                break;
                            default:
                                break;
                        }
                        //跳出模块选择的循环
                        if (choose1 == 3) {
                            break;
                        }
                    }
                    break;
                case 2:
                    System.out.println("正在进入管理员系统..");
                    clientManagerLogin();
                    break;
                case 3:
                    signOut();
                    System.out.println("正在退出系统...");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");

            }
        }
    }


    /**
     * 学员登录，账号和密码同时匹配则登录成功
     *
     * @param
     */
    private User ClientUserLogin() throws IOException, ClassNotFoundException {
        User user1 = new User();
        List<User> list = new ArrayList<>();
        System.out.println("请输入您的账户信息：");
        String userName = ClientScanner.getScanner().next();
        user1.setUsername(userName);
        System.out.println("请输入您的密码信息：");
        String password = ClientScanner.getScanner().next();
        user1.setPassword(password);
        list.add(user1);
        UserMessage tum = new UserMessage("userLogin", list);
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送登录指令成功！");
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            return tum.getList().get(0);
        } else {
            System.out.println("账户不存在，请检查后再试！");
        }
        return null;

    }

    /**
     * 自定义成员方法实现客户端修改密码 的功能
     *
     * @param
     * @throws IOException
     */
    private void ClientUpdatePassword() throws IOException, ClassNotFoundException {
        User user = new User();
        List<User> list = new ArrayList<>();
        System.out.println("请输入您要修改的账号：");
        String username = ClientScanner.getScanner().next();
        user.setUsername(username);
        System.out.println("请输入您要修改后的密码");
        String password = ClientScanner.getScanner().next();
        user.setPassword(password);
        list.add(user);
        System.out.println("修改密码的" + user.toString());
        UserMessage tum = new UserMessage("updatePassword", list);
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送学员修改密码功能成功！");
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("修改密码成功!");
        } else {
            System.out.println("修改密码失败！");
        }
        return;
    }


    /**
     * 自定义成员方法实现客户端管理员登录的功能
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException, IdException, AgeException {
        List<User> list = new ArrayList<>();
        // 1.准备管理员登录的相关数据
        System.out.println("请输入管理员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        list.add(new User(userName, password));
        UserMessage tum = new UserMessage("managerCheck", list);
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送管理员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            while (true) {
                System.out.println("-----------模块选择----------");
                System.out.println("       1 ：学员管理模块       ");
                System.out.println("       2 ：考题管理模块       ");
                System.out.println("       3 ：返回             ");
                System.out.println("----------------------------");
                int choose = ClientScanner.getScanner().nextInt();
                switch (choose) {
                    case 1:
                        System.out.println("正在进入学员系统...");
                        //读取位于User.txt文件下的对象，并返回到list中
                        while (true) {
                            System.out.println("-------学员信息管理系统-------");
                            System.out.println("       1 ：添加学员信息       ");
                            System.out.println("       2 ：删除学员信息       ");
                            System.out.println("       3 ：修改学员信息       ");
                            System.out.println("       4 ：查询学员信息       ");
                            System.out.println("       5 ：遍历学员信息       ");
                            System.out.println("       6 ：退出学员系统       ");
                            System.out.println("---------------------------");
                            System.out.println("请输入您要选择的功能的序号");
                            Integer num = ClientScanner.getScanner().nextInt();
                            switch (num) {
                                case 1:
                                    add();
                                    break;
                                case 2:
                                    delete();
                                    break;
                                case 3:
                                    update();
                                case 4:
                                    find();
                                    break;
                                case 5:
                                    getAllUsers();
                                    break;
                                case 6:
                                    break;
                                default:
                                    break;
                            }
                            if (num == 6) {
                                break;
                            }
                        }
                        break;
                    case 2:
                        System.out.println("正在进入考题管理系统...");
                        while (true) {
                            System.out.println("-------考题管理系统--------");
                            System.out.println("       1 ：增加考题        ");
                            System.out.println("       2 ：删除考题        ");
                            System.out.println("       3 ：修改考题        ");
                            System.out.println("       4 ：查询考题        ");
                            System.out.println("       5 ：返回           ");
                            System.out.println("-------------------------");
                            System.out.println("请输入您要选择的功能的序号");
                            Integer num = ClientScanner.getScanner().nextInt();
                            switch (num) {
                                case 1:
                                    addExam();
                                    break;
                                case 2:
                                    deleteExam();
                                    break;
                                case 3:
                                    updateExam();
                                case 4:
                                    findExam();
                                    break;
                                case 5:
                                    break;
                                default:
                                    break;
                            }
                            if (num == 5) {
                                break;
                            }
                        }
                        break;
                    case 3:
                        return;
                    default:
                        break;
                }
            }
        } else {
            System.out.println("用户名或密码错误！");
        }
    }


    /**
     * 新增学员对象,考虑学号赋值是否合理，是否重复
     *
     * @return
     * @throws IdException
     */
    public void add() throws IdException, IOException, ClassNotFoundException {
        //控制是否继续新增学员
        String isContinue = "Y";
        User user = new User();
        while ("Y".equals(isContinue)) {
            List<User> list = new ArrayList<>();
            System.out.println("请输入您要添加的学号：");
            int id = ClientScanner.getScanner().nextInt();
            user.setId(id);
            System.out.println("请输入您要添加的姓名：");
            String name = ClientScanner.getScanner().next();
            user.setName(name);
            System.out.println("请输入您要添加的年龄：");
            int age = ClientScanner.getScanner().nextInt();
            try {
                user.setAge(age);
            } catch (AgeException e) {
                e.printStackTrace();
            }
            list.add(user);
            UserMessage tum = new UserMessage("addUser", list);
            System.out.println(user.toString());

            cic.getOos().writeObject(tum);
            System.out.println("客户端发送管理员账户信息成功！");
            // 3.接收服务器的处理结果并给出提示
            tum = (UserMessage) cic.getOis().readObject();
            if (("success").equals(tum.getType())) {
                System.out.println("添加成功，是否继续新增？Y/N");
            } else {
                System.out.println("添加失败");
                break;
            }
            isContinue = getString(ClientScanner.getScanner());
        }
        System.out.println("已退出当前添加功能");
    }

    /**
     * 删除学员对象
     *
     * @return
     * @throws IdException
     */
    public void delete() throws IdException, IOException, ClassNotFoundException {
        String isContinue = "Y";
        User user = new User();
        while ("Y".equals(isContinue)) {
            List<User> list = new ArrayList<>();
            System.out.println("请输入待删除学员的学号：");
            Integer id = ClientScanner.getScanner().nextInt();
            user.setId(id);
            list.add(user);
            UserMessage tum = new UserMessage("delete", list);
            cic.getOos().writeObject(tum);
            System.out.println("客户端发送删除学员信息成功！");
            tum = (UserMessage) cic.getOis().readObject();
            if (tum.getType() == "success") {
                System.out.println("是否继续删除？Y/N");
            } else {
                System.out.println("删除失败");
                break;
            }

            isContinue = getString(ClientScanner.getScanner());
            break;
        }
        System.out.println("已退出当前删除功能");
    }


    /**
     * 查找学员
     *
     * @return
     */
    public void find() throws IdException, IOException, ClassNotFoundException {
        //是否继续
        String isContinue = "Y";
        User user = new User();

        System.out.println("请输入您要选择的功能模块：");
        System.out.println("      1.查找学员       ");
        System.out.println("      2.返回          ");
        Integer choose = ClientScanner.getScanner().nextInt();
        switch (choose) {
            case 1:
                while ("Y".equals(isContinue)) {
                    List<User> list = new ArrayList<>();
                    System.out.println("请输入您要查找的学员的学号：");
                    Integer id = ClientScanner.getScanner().nextInt();
                    user.setId(id);
                    list.add(user);
                    UserMessage tum = new UserMessage("select", list);
                    cic.getOos().writeObject(tum);
                    System.out.println("客户端发送查询学员信息成功！");
                    tum = (UserMessage) cic.getOis().readObject();
                    if ("success".equals(tum.getType())) {
                        System.out.println("查找成功!" + tum.getList().get(0).toString());
                        System.out.println("是否继续查找？Y/N");
                    } else {
                        System.out.println("查找失败！");
                        break;
                    }
                    isContinue = getString(ClientScanner.getScanner());
                }
                break;
            case 2:
                return;
            default:
                break;
        }
    }

    /**
     * 修改学员信息中的查找功能
     *
     * @throws IOException
     * @throws IdException
     * @throws ClassNotFoundException
     */
    public void update() throws IOException, IdException, ClassNotFoundException, AgeException {
        String isContinue = "Y";
        User user = new User();
        System.out.println("请输入您要选择的功能模块：");
        System.out.println("      1.查找学员       ");
        System.out.println("      2.返回          ");
        Integer choose = ClientScanner.getScanner().nextInt();
        switch (choose) {
            case 1:
                while ("Y".equals(isContinue)) {
                    List<User> list = new ArrayList<>();
                    System.out.println("请输入您要查找的学员的学号：");
                    Integer id = ClientScanner.getScanner().nextInt();
                    user.setId(id);
                    list.add(user);
                    UserMessage tum = new UserMessage("select", list);
                    cic.getOos().writeObject(tum);
                    System.out.println("客户端发送查询学员信息成功！");
                    tum = (UserMessage) cic.getOis().readObject();
                    User userBack = tum.getList().get(0);
                    if ("success".equals(tum.getType())) {
                        System.out.println("查找成功!学员信息为" + userBack.toString());
                        int i = modify(userBack);
                        //判断是否是返回的功能，如果是则直接返回至查找学员/返回功能，跳过请输入您要查找的学员的学号
                        if (i == 3) {
                            return;
                        }
                    } else {
                        System.out.println("查找失败！");
                        break;
                    }
                }
                break;
            case 2:
                return;
            default:
                break;
        }
    }

    /**
     * 更新学员信息
     *
     * @param user
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws AgeException
     */
    public int modify(User user) throws IOException, ClassNotFoundException, AgeException {
        //是否继续
        String isContinue = "Y";
        while ("Y".equals(isContinue)) {
            System.out.println("请输入您要修改的信息类型：");
            System.out.println("       1.修改姓名      ");
            System.out.println("       2.修改年龄      ");
            System.out.println("       3.返回         ");
            Integer type = ClientScanner.getScanner().nextInt();
            switch (type) {
                case 1:
                    modifyUserName(user);
                    break;
                case 2:
                    modifyUserAge(user);
                    break;
                case 3:
                    return 3;
                default:
                    break;

            }
        }
        return 0;
    }

    /**
     * 修改学员姓名
     *
     * @param user
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void modifyUserName(User user) throws IOException, ClassNotFoundException {
        List<User> list = new ArrayList<>();
        System.out.println("请输入您要修改后的姓名：");
        String name = ClientScanner.getScanner().next();
        user.setName(name);
        list.add(user);
        UserMessage tum = new UserMessage("update", list);
        cic.getOos().writeObject(tum);
        tum = (UserMessage) cic.getOis().readObject();
        User userBack = tum.getList().get(0);
        if ("success".equals(tum.getType())) {
            System.out.println("修改成功！修改后的学员信息为：" + userBack.toString());
        } else {
            System.out.println("修改失败");
        }
        return;
    }


    /**
     * 修改学员年龄
     *
     * @param user
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws AgeException
     */
    public void modifyUserAge(User user) throws IOException, ClassNotFoundException, AgeException {
        List<User> list = new ArrayList<>();
        System.out.println("请输入您要修改后的年龄：");
        Integer age = ClientScanner.getScanner().nextInt();
        user.setAge(age);
        list.add(user);
        UserMessage tum = new UserMessage("update", list);
        cic.getOos().writeObject(tum);
        tum = (UserMessage) cic.getOis().readObject();
        User userBack = tum.getList().get(0);
        if ("success".equals(tum.getType())) {
            System.out.println("修改成功！修改后的学员信息为：" + userBack.toString());
        } else {
            System.out.println("修改失败");
        }
        return;
    }


    public void getAllUsers() throws IOException, ClassNotFoundException {
        UserMessage tum = new UserMessage("getAll", null);
        cic.getOos().writeObject(tum);
        tum = (UserMessage) cic.getOis().readObject();
        List<User> list = tum.getList();
        System.out.println("学员信息有:" + list.toString());
        return;
    }

    public void addExam() throws IOException, ClassNotFoundException {
        String isContinue = "Y";
        Exam exam = new Exam();
        while ("Y".equals(isContinue)) {
            List<Exam> list = new ArrayList<>();
            System.out.println("请输入您要添加的题目号：");
            int id = ClientScanner.getScanner().nextInt();
            exam.setId(id);
            System.out.println("请输入您要添加的问题：");
            String question = ClientScanner.getScanner().next();
            exam.setQuestion(question);
            System.out.println("请输入您要添加的答案：");
            String answer = ClientScanner.getScanner().next();
            exam.setAnswer(answer);
            list.add(exam);
            ExamMessage tum = new ExamMessage("addExam", list);
            System.out.println(exam.toString());

            cic.getOos().writeObject(tum);
            System.out.println("客户端发送管理员账户信息成功！");
            // 3.接收服务器的处理结果并给出提示
            tum = (ExamMessage) cic.getOis().readObject();
            if (("success").equals(tum.getType())) {
                System.out.println("添加成功，是否继续新增？Y/N");
            } else {
                System.out.println("添加失败");
                break;
            }
            isContinue = getString(ClientScanner.getScanner());
        }
        System.out.println("已退出当前添加功能");
    }

    public void deleteExam() throws IOException, ClassNotFoundException {
        String isContinue = "Y";
        Exam exam = new Exam();
        while ("Y".equals(isContinue)) {
            List<Exam> list = new ArrayList<>();
            System.out.println("请输入待删除题目的题号：");
            Integer id = ClientScanner.getScanner().nextInt();
            exam.setId(id);
            list.add(exam);
            ExamMessage tum = new ExamMessage("delete", list);
            cic.getOos().writeObject(tum);
            System.out.println("客户端发送删除题目信息成功！");
            tum = (ExamMessage) cic.getOis().readObject();
            if (tum.getType() == "success") {
                System.out.println("是否继续删除？Y/N");
            } else {
                System.out.println("删除失败");
                break;
            }

            isContinue = getString(ClientScanner.getScanner());
            break;
        }
        System.out.println("已退出当前删除功能");
    }

    public void findExam() throws IOException, ClassNotFoundException {
        //是否继续
        String isContinue = "Y";
        Exam exam = new Exam();

        System.out.println("请输入您要选择的功能模块：");
        System.out.println("      1.查找考题      ");
        System.out.println("      2.返回          ");
        Integer choose = ClientScanner.getScanner().nextInt();
        switch (choose) {
            case 1:
                while ("Y".equals(isContinue)) {
                    List<Exam> list = new ArrayList<>();
                    System.out.println("请输入您要查找的题目的题号：");
                    Integer id = ClientScanner.getScanner().nextInt();
                    exam.setId(id);
                    list.add(exam);
                    ExamMessage tum = new ExamMessage("selectExam", list);
                    cic.getOos().writeObject(tum);
                    System.out.println("客户端发送查询题目信息成功！");
                    tum = (ExamMessage) cic.getOis().readObject();
                    if ("success".equals(tum.getType())) {
                        System.out.println("查找成功!" + tum.getList().get(0).toString());
                        System.out.println("是否继续查找？Y/N");
                    } else {
                        System.out.println("查找失败！");
                        break;
                    }
                    isContinue = getString(ClientScanner.getScanner());
                }
                break;
            case 2:
                return;
            default:
                break;
        }
    }

    /**
     * 更新考题中，先查找考题的功能
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void updateExam() throws IOException, ClassNotFoundException {
        String isContinue = "Y";
        Exam exam = new Exam();
        System.out.println("请输入您要选择的功能模块：");
        System.out.println("      1.查找考题       ");
        System.out.println("      2.返回          ");
        Integer choose = ClientScanner.getScanner().nextInt();
        switch (choose) {
            case 1:
                while ("Y".equals(isContinue)) {
                    List<Exam> list = new ArrayList<>();
                    System.out.println("请输入您要查找的学员的学号：");
                    Integer id = ClientScanner.getScanner().nextInt();
                    exam.setId(id);
                    list.add(exam);
                    ExamMessage tum = new ExamMessage("selectExam", list);
                    cic.getOos().writeObject(tum);
                    System.out.println("客户端发送查询题目信息成功！");
                    tum = (ExamMessage) cic.getOis().readObject();
                    Exam examBack = tum.getList().get(0);
                    if ("success".equals(tum.getType())) {
                        System.out.println("查找成功!学员信息为" + examBack.toString());
                        int i = modifyExam(examBack);
                        //判断是否是返回的功能，如果是则直接返回至查找学员/返回功能，跳过请输入您要查找的学员的学号
                        if (i == 3) {
                            return;
                        }
                    } else {
                        System.out.println("查找失败！");
                        break;
                    }
                }
                break;
            case 2:
                return;
            default:
                break;
        }
    }

    /**
     * 修改考题中的信息
     *
     * @param exam
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public int modifyExam(Exam exam) throws IOException, ClassNotFoundException {
        //是否继续
        String isContinue = "Y";
        while ("Y".equals(isContinue)) {
            System.out.println("请输入您要修改的信息类型：");
            System.out.println("       1.修改问题      ");
            System.out.println("       2.修改答案      ");
            System.out.println("       3.返回         ");
            Integer type = ClientScanner.getScanner().nextInt();
            switch (type) {
                case 1:
                    modifyExamQuestion(exam);
                    break;
                case 2:
                    modifyExamAnswer(exam);
                    break;
                case 3:
                    return 3;
                default:
                    break;

            }
        }
        return 0;
    }

    /**
     * 修改考题中的问题
     *
     * @param exam
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void modifyExamQuestion(Exam exam) throws IOException, ClassNotFoundException {
        List<Exam> list = new ArrayList<>();
        System.out.println("请输入您要修改后的问题：");
        String question = ClientScanner.getScanner().next();
        exam.setQuestion(question);
        list.add(exam);
        ExamMessage tum = new ExamMessage("updateExam", list);
        cic.getOos().writeObject(tum);
        tum = (ExamMessage) cic.getOis().readObject();
        Exam examBack = tum.getList().get(0);
        if ("success".equals(tum.getType())) {
            System.out.println("修改成功！修改后的题目信息为：" + examBack.toString());
        } else {
            System.out.println("修改失败");
        }
        return;
    }

    /**
     * 修改考题中的答案
     *
     * @param exam
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void modifyExamAnswer(Exam exam) throws IOException, ClassNotFoundException {
        List<Exam> list = new ArrayList<>();
        System.out.println("请输入您要修改后的答案：");
        String answer = ClientScanner.getScanner().next();
        exam.setAnswer(answer);
        list.add(exam);
        ExamMessage tum = new ExamMessage("updateExam", list);
        cic.getOos().writeObject(tum);
        tum = (ExamMessage) cic.getOis().readObject();
        Exam examBack = tum.getList().get(0);
        if ("success".equals(tum.getType())) {
            System.out.println("修改成功！修改后的题目信息为：" + examBack.toString());
        } else {
            System.out.println("修改失败");
        }
        return;
    }

    /**
     * 客户端获取所有考题
     *
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public List<Exam> getAllExams() throws IOException, ClassNotFoundException {
        System.out.println("正在获取所有题目i..");
        ExamMessage tum = new ExamMessage("getAllExams", null);
        cic.getOos().writeObject(tum);
        tum = (ExamMessage) cic.getOis().readObject();
        List<Exam> list = tum.getList();
        return list;
    }

    /**
     * 开始考试：1.将学员做后的试题上传，得到分数；2.将分数放在user中，再次上传
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void startExam() throws IOException, ClassNotFoundException {

        User user = ClientUserLogin();
        if (null != user) {
            System.out.println("正在发题，请稍候..");
            List<Exam> list = getAllExams();
            List<Exam> list1 = new ArrayList<>();
            System.out.println("考题有" + list.size() + "道");
            for (int i = 0; i < list.size(); i++) {
                Exam exam = list.get(i);
                System.out.println("第" + i+1 + "题");
                System.out.println(exam.getQuestion());
                System.out.println("请输入您的答案：");
                String answer = ClientScanner.getScanner().next();
                exam.setAnswer(answer);
                list1.add(exam);
            }
            ExamMessage tum = new ExamMessage("startExam", list1);
            cic.getOos().writeObject(tum);
            tum = (ExamMessage) cic.getOis().readObject();
            System.out.println("您的成绩为" + tum.getType() + "分");
            user.setGrade(Integer.parseInt(tum.getType()));
            List<User> listUser = new ArrayList<>();
            listUser.add(user);
            UserMessage userMessage = new UserMessage("update", listUser);
            cic.getOos().writeObject(userMessage);
            userMessage = (UserMessage) cic.getOis().readObject();
            if ("success".equals(userMessage.getType())) {
                System.out.println("成绩上传成功！");
            } else {
                System.out.println("成绩上传失败！");
            }

        }
    }

    /**
     * 通过学号查询成绩
     * @throws IdException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void selectGrade() throws IdException, IOException, ClassNotFoundException {
        String isContinue = "Y";
        while ("Y".equals(isContinue)) {
            User user = new User();
            List<User> list = new ArrayList<>();
            System.out.println("请输入您要查询的学员的学号：");
            Integer id = ClientScanner.getScanner().nextInt();
            user.setId(id);
            list.add(user);
            UserMessage tum = new UserMessage("select", list);
            cic.getOos().writeObject(tum);
            System.out.println("客户端发送查询学员成绩成功！");
            tum = (UserMessage) cic.getOis().readObject();
            if ("success".equals(tum.getType())) {
                System.out.println("查找成功!" + tum.getList().get(0).toString());
                System.out.println("是否继续查找？Y/N");
            } else {
                System.out.println("查找失败！");
                break;
            }
            isContinue = getString(ClientScanner.getScanner());
        }


    }

    /**
     * 客户端发送指令退出系统
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void signOut() throws IOException, ClassNotFoundException {
        UserMessage tum=new UserMessage("signOut",null);
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送退出系统成功！");
        tum=(UserMessage)cic.getOis().readObject();
        if("success".equals(tum.getType())){
            System.out.println("系统退出成功");
            return;
        }
    }

    /**
     * 判断流程输入值是否合理
     *
     * @param scanner
     * @return
     */
    public static String getString(Scanner scanner) {
        String isContinue;
        while (true) {
            String s = scanner.next();
            if ("Y".equals(s) || "N".equals(s)) {
                isContinue = s;
                break;
            } else {
                System.out.println("输入不合法，请重新输入！");
            }
        }
        return isContinue;
    }
}
